package com.hhkj.study.common.service.impl;

import cn.felord.callbacks.CallbackCrypto;
import cn.felord.domain.contactbook.tag.TagUser;
import cn.hutool.core.util.ObjectUtil;
import com.hhkj.study.common.service.WeComService;
import com.ruoyi.common.thirdparty.wecom.WeComApiClient;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author luowu
 * @Date 2024/9/14 涵 13:29:21
 */
@Service
public class WeComServiceImpl implements WeComService {

    @Resource
    private CallbackCrypto callbackCrypto;

    @Resource
    private WeComApiClient weComApiClient;

    /** 默认企微标签id */
    private static final Integer DEFAULT_TAG_ID = 1;

    @Override
    public String verifyUrl(String agentId, String corpId, String msgSignature, String timestamp, String nonce, String echostr) {
        return callbackCrypto.decryptMsg(agentId, corpId, msgSignature, timestamp, nonce, echostr);
    }

    @Override
    public String consumeCallback(String agentId, String corpId, String msgSignature, String timestamp, String nonce, String xmlBody) {
        return callbackCrypto.accept(agentId, corpId, msgSignature, timestamp, nonce, xmlBody);
    }

    @Override
    public List<TagUser> weComUserList(Integer tagId) {
        if (ObjectUtil.isNull(tagId)) {
            tagId = DEFAULT_TAG_ID;
        }
        return weComApiClient.getUserIdListByTag(tagId);
    }
}
